package com.kingbull.web.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

//main mapper
public interface Mapper {

    @Select("select count(1) from js_sys_user where user_code = #{code}")
    Object queryUserById(String code);

    @Insert("insert into t_all_in_one_card_log(jsonstr,type) values(#{str},#{type})")
    int saveLog(@Param("str") String str,@Param("type")  String type);

    @Select("select o.id,\n" +
            "\t\t\to.orderType,\n" +
            "\t\t\to.incoming_state AS state\n" +
            "from t_sales_order o\n" +
            "where o.orderType = IFNULL((\n" +
            "select case when xs>0 then 1 when ycl>0 then 2 when dl>0 then 3 when qt>0 then 4 else -1 end from(select\n" +
            "\t\tIFNULL(SUM(case when orderType=1 then 1 else 0 end ),0) xs,\n" +
            "\t\tIFNULL(SUM(case when orderType=2 then 1 else 0 end ),0) ycl,\n" +
            "\t\tIFNULL(SUM(case when orderType=3 then 1 else 0 end ),0) dl,\n" +
            "\t\tIFNULL(SUM(case when orderType=4 then 1 else 0 end ),0) qt\n" +
            "\t\tfrom t_sales_order\n" +
            "\t\twhere idCardNo = #{code}\n" +
            "\t\tand 'status' = 0\n" +
            "\t\tand quite_flag = 0\n" +
            "\t\tand company_code = #{company}\n" +
            "\t\tand (TIMESTAMPDIFF(MINUTE,create_date,SYSDATE())/60 < 24 or incoming_state = 1)\n" +
            "\t)x\n" +
            "),-1) and o.idCardNo = #{code} and o.status = 0 and o.quite_flag = 0 and o.company_code = #{company}\n" +
            "and (TIMESTAMPDIFF(MINUTE,o.create_date,SYSDATE())/60 < 24 or o.incoming_state = 1)")
    Map<String,Object> getOrder(@Param("code") String code,@Param("company")  String company);

    @Update("update t_sales_order set incoming_state=1 where id= #{id}")
    int updateIncomingState(String id);
}
